/**
 * 
 */
package com.thor.palmmall.impl.elecharges.job;

import java.rmi.RemoteException;
import java.util.Date;
import java.util.List;

import javax.xml.rpc.ServiceException;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query2.QueryDefinition2;
import com.thor.palmmall.api.elecharges.EleChargesBill;
import com.thor.palmmall.api.elecharges.EleChargesBillService;
import com.thor.palmmall.api.elecharges.EleChargesBillState;
import com.thor.palmmall.impl.elecharges.WebServiceClient;
import com.thor.palmmall.util.pay.tool;
import com.thor.util.ApplicationContextUtils;

/**
 * @author NuoSa
 *
 * @since 1.0
 */
@DisallowConcurrentExecution
public class ChargesJob extends QuartzJobBean {

  @Override
  protected void executeInternal(JobExecutionContext paramJobExecutionContext)
      throws JobExecutionException {
    QueryDefinition2 definition = new QueryDefinition2();
    definition.addCondition(EleChargesBillService.CONDITION_STATE_IN, EleChargesBillState.PAID);
    definition.addCondition(EleChargesBillService.CONDITION_NOTIFYFLAG_EQUALS, "success");
    definition.setPageNumber(0);
    definition.setPageSize(100);
    List<EleChargesBill> list = getEleChargesService().query(definition).getRecords();
    for (EleChargesBill bill : list) {
      try {
        String isSuccess = getWebServiceClientService().callPut(bill);
        if ("success".equals(isSuccess)) {
          bill.setActionTime(new Date());
          bill.setState(EleChargesBillState.FINISH);
          try {
            getEleChargesService().saveEleChargesBill(bill, tool.getOperateInfo());
          } catch (ThorServiceException e) {
            e.printStackTrace();
          }
        }
      } catch (RemoteException e) {
        e.printStackTrace();
      } catch (ServiceException e) {
        e.printStackTrace();
      }
    }
  }

  private EleChargesBillService getEleChargesService() {
    return ApplicationContextUtils.getBean(EleChargesBillService.class);
  }

  private WebServiceClient getWebServiceClientService() {
    return ApplicationContextUtils.getBean(WebServiceClient.class);
  }

}
